<template>
	<view class="content">

		<u-form :model="userInfo" ref="form1" class="user-list list-border">
			<!-- <view class="" style="background-color:rgba(220, 20, 60, 0.2);padding: 10%;">
				温馨提示：<br />
				1、请确保真实性，小儿的身份证号码要准确。<br />
				2、家长信息要完善。
			</view> -->
			<u-form-item label="姓名" prop="TJName" label-width="150" required="true">
				<u-input placeholder="请填写姓名" v-model="userInfo.TJName" />
			</u-form-item>
			<u-form-item label="身份证号" prop="TJIDCardNo" label-width="150" required="true">
				<u-input placeholder="请填写身份证" :maxlength="18" v-model="userInfo.TJIDCardNo" />
			</u-form-item>
			<u-form-item label="手机号码" prop="TJPhoneNo" label-width="150" required="true">
				<u-input placeholder="请填写手机号" :maxlength="11" v-model="userInfo.TJPhoneNo" />
			</u-form-item>
		<!-- 	<u-form-item label="家长姓名" prop="TJPName" label-width="150" required="true">
				<u-input placeholder="请填写家长姓名" v-model="userInfo.TJPName" />
			</u-form-item>
			<u-form-item label="家长身份证号" label-width="180">
				<u-input placeholder="请填写家长身份证号" :maxlength="18" v-model="userInfo.TJPIDCard" />
			</u-form-item> -->
			<!-- 校验待修改 -->
		<!-- 	<u-form-item label="幼儿园" prop="" label-width="150" required="true">
				<u-input v-model="selectKinder.current.label" placeholder="请选择幼儿园" type="select"
					@click="selectKinder.show=true" />
			</u-form-item>
			<u-form-item label="年级" label-width="150">
				<u-input v-model="selectGrade.current.label" placeholder="请选择年级" type="select"
					@click="selectGrade.show=true" />
			</u-form-item> -->
			<!-- <u-form-item label="班级" label-width="150">
				<u-input placeholder="请填写班级" v-model="userInfo.TJClassName" />
			</u-form-item> -->
			<u-form-item label="所在地区"  prop="region"   required="true" :border-bottom="false" label-width="150">
				<u-input v-model="userInfo.region" placeholder="请选择所在地区" @focus="change()" />
				<u-picker   class="region" mode="region" v-model="show" @confirm="confirmArea"  :area-code='["130000", "130900", "130984"]'></u-picker>
			</u-form-item>
			<u-form-item label="家庭住址" prop="TJAddress" label-width="150" required="true">
				<u-input placeholder="请填写地址(例:XX小区X号楼X单元X室)" v-model="userInfo.TJAddress" />
			</u-form-item>
			<!-- 校验待修改 -->
			<u-form-item label="既往病史"  label-width="150" >
				<u-input v-model="selectCase.current.label" placeholder="请选择既往病史" type="select"
					@click="selectCase.show=true" />
			</u-form-item>
			<u-form-item label="其他既往史" label-width="150">
				<u-input placeholder="请填写其他既往史" v-model="userInfo.TJOtherIll" />
			</u-form-item>
			<u-form-item label="过敏史" label-width="150">
				<u-input placeholder="请填写过敏史" v-model="userInfo.TJAllergies" />
			</u-form-item>
		</u-form>
		<u-select v-model="select1.show" :list="select1.list" @confirm="onSelect1"></u-select>
	<!-- 	<u-select v-model="selectKinder.show" :list="selectKinder.list" @confirm="OnselectKinder"></u-select> -->
		<u-select v-model="selectCase.show" :list="selectCase.list" @confirm="OnselectCase"></u-select>
		<!-- <u-select v-model="selectGrade.show" :list="selectGrade.list" @confirm="OnselectGrade"></u-select> -->
		<view class="bottom-button">
			<!-- <u-button shape="circle" type="primary" size="default" @click="submit">提交</u-button> -->
			<view class="button bacnone2" @click="submit">
				<span>确定添加</span>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {
				defaultRegion:['河北省','沧州市','河间市'],
				sexs: '请选择',
				show: false,
				TJName: '',
				TJIDCardNo: '',
				TJPhoneNo: '',
				// TJPName: '',
				Kinder: {},
				TJAddress: '',
				TJIllness: '',
				His: '',
				userInfo: {
					TJName: '',
					TJCounty: '',
					TJIDCardNo: '',
					TJPhoneNo: '',
					// TJPName: '',
					// TJPIDCard: '',
					TJClassName: '',
					TJAddress: '',
					TJAllergies: '',
					region: '',
					TJProvince: '',
					TJCity: '',
					TJCounty: '',
					TJKinderCode: '',
					TJGradeCode: '',
					TJIllness: '',
					TJOtherIll: '',
					TJChildId: '',
				},

				select1: {
					show: false,
					current: {},
					list: [{
							value: 1,
							label: '男'
						},
						{
							value: 2,
							label: '女'
						}
					],
				},
				selectCase: {
					show: false,
					current: {},
					list: [],
				},
				selectGrade: {
					show: false,
					current: {},
					list: [],
				},
				selectKinder: {
					show: false,
					current: {},
					list: [],
				},
				useyzm: '',
				rules: {
					Kinder: [{
						required: true,
						message: '请选择幼儿园',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],

					TJName: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					TJPName: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					His: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					TJAddress: [{
						required: true,
						message: '请填写家庭住址',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					sex: [{
						required: true,
						message: '请选择性别',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					TJIDCardNo: [{
							required: true,
							message: '请输入身份证',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						},
						{
							len: 18,
							message: '请正确输入18位身份证',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}
					],

					TJPIDCard: [{
							required: true,
							message: '请输入家长姓名',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						},
						{
							len: 18,
							message: '请正确输入18位身份证',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}
					],
					region: [{
							required: true,
							message: '请选择区域',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}
						
					],
					TJPhoneNo: [{
							required: true,
							message: '请输入就诊人手机号',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						},
						{
							len: 11,
							message: '请正确输入11位手机号',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}
					]
				},
			}
		},
		watch: {
			value(newValue, oldValue) {
				//console.log('v-model', newValue);
			}
		},
		computed: {
			...mapState(['token'], ['userInfo']),
			...mapGetters(['hasLogin', 'getUserInfo']),
			getIcon() {
				return path => {
					return 'https://cdn.uviewui.com/uview/example/' + path + '.png';
				}
			}
		},
		     onBackPress(event) {
		     	uni.switchTab({
		     		url: '/pages/index/index'
		     	});
		     	return true;
		     },
		onLoad(options) {

			if (options.PatientId != undefined) {
				this.userInfo.TJChildId = options.PatientId
				console.log("刚开始加载" + options.PatientId)

				this.getTJChildId()

				uni.hideLoading()
			}

		},




		onShow() {
			if (this.hasLogin) {

			} else {

				uni.showModal({
					title: '提示',
					content: '登录后才可以操作',
					confirmText: '去登录',
					showCancel: false,
					success: function(res) {
						uni.reLaunch({
							url: '/pages/auth/login'
						})
					}
				});
				return;
			}

		},


		onReady() {
			this.$refs.form1.setRules(this.rules);
		},

		created() {
			this.getKinder();
			this.getCase();
			this.getGrade();
		},

		methods: {
			onSelect1(arr) {
				let current = arr[0];
				let label = current.label;
				this.select1.current = current;
				this.userInfo.sex = label;

			},
			OnselectKinder(arr) {
				let current = arr[0];
				let label = current.label;
				this.selectKinder.current = current;
				//this.userInfo.sex = label;
				this.userInfo.TJKinderCode = current.extra;
				console.log(this.userInfo.TJKinderCode)
			},
			OnselectCase(arr) {
				let current = arr[0];
				let label = current.label;
				this.selectCase.current = current;


				//	console.log(this.selectCase.current.code)
			},
			OnselectGrade(arr) {
				console.log(arr[0])
				let current = arr[0];
				let label = current.label;



				this.selectGrade.current = current;
				//	this.userInfo.sex = label;
				this.userInfo.TJGradeCode = current.extra;
				console.log(this.selectGrade.list)
				console.log(this.userInfo.TJGradeCode)
				//	this.userInfo.TJGradeCode=
				//console.log("this.userInfo.sex", this.userInfo.sex)
			},
			change() {
				this.show = true
			},

			confirmArea(e) {
				// 省市区拼接

				this.userInfo.region = e.province.name + " " + e.city.name + " " + e.area.name
				this.userInfo.province = e.province.name
				this.userInfo.city = e.city.name
				this.userInfo.area = e.area.name
				console.log(this.userInfo.region)
				console.log(this.userInfo.province)
				console.log(this.userInfo.city)
				console.log(this.userInfo.area)
				console.log(e)
			},
			getKinder() {
				this.$request("TJChildBase", "GetKinder", {
					Code: '',
				}).then(res => {

					if (res.code == 1) {
						this.selectKinder.list = []
						for (let i = 0; i < res.data.length; i++) {

							let list = {
								value: res.data[i].TJStatus,
								label: res.data[i].TJName,
								extra: res.data[i].TJCode,
							}

							this.selectKinder.list.push(list)

						}

						// uni.hideLoading()
						// this.Organshow = true
					}

				})

			},
			getTJChildId() {
				this.$request("TJChildBase", "GetchildBase", {
					TJChildId: this.userInfo.TJChildId,
				}).then(res => {

					if (res.code == 1) {

						this.userInfo.TJChildId = res.data.TJChildId
						this.userInfo.TJName = res.data.TJName
						this.userInfo.TJIDCardNo = res.data.TJIDCardNo
						this.userInfo.TJPhoneNo = res.data.TJPhoneNo
						// this.userInfo.TJPName = res.data.TJPName
						// this.userInfo.TJPIDCard = res.data.TJPIDCard
						this.userInfo.TJKinderCode = res.data.TJKinderCode
						// this.selectKinder.current.label = res.data.TJKinderName
						this.userInfo.TJGradeCode = res.data.TJGradeCode
						// this.selectGrade.current.label = res.data.TJGradeName
						// this.userInfo.TJClassName = res.data.TJClassName
						this.userInfo.province = res.data.TJProvince
						this.userInfo.city = res.data.TJCity
						this.userInfo.area = res.data.TJCounty
						this.userInfo.TJAddress = res.data.TJAddress
						this.selectCase.current.label = res.data.TJIllness
						this.userInfo.TJOtherIll = res.data.TJOtherIll
						this.userInfo.TJAllergies = res.data.TJAllergies

						// uni.hideLoading()
						// this.Organshow = true
					}

				})

			},

			getCase() {

				this.$request("dictionary", "list", {
					code: 'jwbs',
				}).then(res => {

					if (res.code == 1) {
						this.selectCase.list = []
						for (let i = 0; i < res.data.length; i++) {
							let list = {
								value: res.data[i].SortCode,
								label: res.data[i].FullName,
							}

							this.selectCase.list.push(list)

						}
						// uni.hideLoading()
						// this.Organshow = true
					}

				})

			},
			getGrade() {


				this.$request("dictionary", "list", {
					Code: 'Nj',
				}).then(res => {

					if (res.code == 1) {
						this.selectGrade.list = []
						for (let i = 0; i < res.data.length; i++) {
							let list = {
								value: res.data[i].SortCode,
								label: res.data[i].FullName,
								extra: res.data[i].Code,
							}

							this.selectGrade.list.push(list)

						}
						// uni.hideLoading()
						// this.Organshow = true
					}

				})

			},
			submit() {
				this.$refs.form1.validate(valid => {
					if (valid) {
						this.addvisit()
						console.log('验证通过');
					} else {
						console.log('验证失败');
					}
				});
			},
			cancel() {
				this.show = false
			},
			confirm(e) {
				this.sexs = e.value[0]
				this.userInfo.sex = e.value[0]
				this.show = false
			},
			addvisit() {
				console.log(this.userInfo)
				uni.showLoading({
					title: '提交中'
				})
				let user = this.userInfo
				this.$request('TJChildBase', 'AddchildBase', {
					TJChildId: this.userInfo.TJChildId,
					TJName: this.userInfo.TJName,
					TJIDCardNo: this.userInfo.TJIDCardNo,
					TJPhoneNo: this.userInfo.TJPhoneNo,
					TJPName: this.userInfo.TJPName,
					TJPIDCard: this.userInfo.TJPIDCard,
					// TJKinderCode: this.userInfo.TJKinderCode,
					// TJKinderName: this.selectKinder.current.label,
					TJGradeCode: this.userInfo.TJGradeCode,
					TJGradeName: this.selectGrade.current.label,
					TJClassName: this.userInfo.TJClassName,
					TJProvince: this.userInfo.province,
					TJCity: this.userInfo.city,
					TJCounty: this.userInfo.area,
					TJAddress: this.userInfo.TJAddress,
					TJIllness: this.selectCase.current.label,
					TJOtherIll: this.userInfo.TJOtherIll,
					TJAllergies: this.userInfo.TJAllergies,
				}).then(res => {
					console.log("登录成功了!!!!!!!!!")
					if (res.code == 1) {
						let doctorinfor = false
						this.userInfo.TJChildId = ''
						uni.hideLoading();
						uni.redirectTo({
							url: `/pages/MedicalExamination/MedicalExamination`,
							fail(error) {
								console.log(error)
							}
						})
					} else {

						console.log("登录失败了")
						uni.hideLoading();
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},



			codeChange(text) {
				this.tips = text;
			},
			getCode() {

				if (this.userInfo.TJPhoneNo == "") {
					this.showToast(this.list)
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			// change(e) {
			// 	console.log('change', e);
			// }
		}
	}
</script>

<style>
	@import './AddUser.css'
</style>